import MusicManager from "../../MusicManager";
import NoviceGuide from "../UI/NoviceGuide";
import OilWell from "../facility/priveteFacility/OilWell";
import Mine from "../facility/pulbicFacility/Mine";
import { FacilityType } from "../info/GameEnum";
import GameManager from "../manager/GameManager";
import PlayerManager from "../manager/PlayerManager";
import UiManager from "../manager/UiManager";
import Dialog from "./Dialog";

const { ccclass, property } = cc._decorator;

/**
 * 升级弹窗
 */
@ccclass
export default class OilMineDialog extends Dialog {

    @property(cc.Node)
    buy: cc.Node = null;

    @property(cc.Node)
    sell: cc.Node = null;

    mine: Mine;

    oilWell: OilWell;

    protected onDisable(): void {
        this.mine?.node.targetOff(this);
        this.oilWell.buildTip.node.targetOff(this);
    }

    public init(facility: Mine | OilWell) {
        this.mine = facility.getComponent(Mine);
        this.oilWell = facility.getComponent(OilWell);
        this.mine.node.on(Mine.EventType.LoseMine, this.resetMine, this);

        this.resetMine();
        this.resetWell();
    }

    resetMine() {
        let progrss = this.getComponentInChildren(cc.ProgressBar);
        progrss.progress = this.mine.curCount / this.mine.maxCount;
        cc.find("bg/progress/balance", this.node).getComponent(cc.Label).string = `${this.mine.curCount}/${this.mine.maxCount}`
    }

    resetWell() {
        if (this.oilWell.player) {
            if (this.oilWell.player == PlayerManager.instance.controllerPlayer) {
                //显示卖出按钮
                this.sell.active = true;
                this.buy.active = false;
                let labels = this.sell.getComponentsInChildren(cc.Label);
                let sellPrice = { diamond: Math.ceil(this.oilWell.price.diamond / 2), oil: Math.ceil(this.oilWell.price.oil / 2) };
                if (sellPrice.diamond) {
                    labels[1].string = sellPrice.diamond.toString();
                } else {
                    labels[1].node.parent.active = false;
                }
                if (sellPrice.oil) {
                    labels[2].string = sellPrice.oil.toString();
                } else {
                    labels[2].node.parent.active = false;
                }
            }
        } else {
            this.sell.active = false;
            this.buy.active = true;
            let labels = this.buy.getComponentsInChildren(cc.Label);
            let price = PlayerManager.instance.controllerPlayer.getPrice(FacilityType.采油机)
            if (price.diamond) {
                labels[1].string = price.diamond.toString();
            } else {
                labels[1].node.parent.active = false;
            }
            if (price.oil) {
                labels[2].string = price.oil.toString();
            } else {
                labels[2].node.parent.active = false;
            }
            if (this.oilWell.buildTip.node.active && PlayerManager.instance.controllerPlayer.diamond >= price.diamond && PlayerManager.instance.controllerPlayer.oil >= price.oil) {
                //显示购买按钮
                this.buy.getComponent(cc.Sprite).setMaterial(0, cc.Material.getBuiltinMaterial('2d-sprite'));
            } else {
                //不显示按钮
                this.buy.getComponent(cc.Sprite).setMaterial(0, cc.Material.getBuiltinMaterial('2d-gray-sprite'));
            }
        }
    }

    onClick(event: cc.Event) {
        let player = PlayerManager.instance.controllerPlayer
        let name = event.target.name;
        MusicManager.instance.play(MusicManager.instance.btnMove);
        switch (name) {
            case "采油机":
                let price = PlayerManager.instance.controllerPlayer.getPrice(FacilityType.采油机)
                if (this.oilWell.machine.node.active) {
                    UiManager.instance.showTip("已被其他人抢先修建了！");
                } else if (!this.oilWell.buildTip.node.active) {
                    UiManager.instance.showTip("不在领地内");
                } else if (PlayerManager.instance.controllerPlayer.diamond < price.diamond) {
                    UiManager.instance.showTip("晶矿不足");
                } else if (PlayerManager.instance.controllerPlayer.oil < price.oil) {
                    UiManager.instance.showTip("油矿不足");
                } else {
                    player.addFacility(FacilityType.采油机, [GameManager.instance.getMapLocation(this.mine.node.position)])
                    player.node.emit(NoviceGuide.EventType.建造设施, FacilityType.采油机);
                }
                break;
            case "卖出":
                if (!this.oilWell.machine.node.active) {
                    UiManager.instance.showTip("已被摧毁了！");
                } else {
                    this.oilWell.die(null);
                    player.diamond += 50;
                }
                break;
        }
        this.closeDialog();
    }

    /**
     * 有玩家建造，立即destroy
     */
    onWellBuild() {
        this.closeDialog();
    }
}
